package com.kakavr.base.listener;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.PropertiesLoaderUtils;

import com.kakavr.base.log.BaseLog;
import com.kakavr.base.resource.SystemResource;

public class BaseListener extends BaseLog implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		SystemResource.clear();
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		debug("加载配置文件");
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			Resource[] resources = resolver.getResources("conf/*");
			for (Resource resource : resources) {
				Properties properties = PropertiesLoaderUtils.loadProperties(resource);
				SystemResource.putAll(properties);
			}
		} catch (IOException e) {
			error(e.getMessage());
		}

	}

}
